﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using BLL;
using System.Web.UI.DataVisualization.Charting;
using System.IO;

namespace OA.Controllers
{
    public class MembercartTypeChartController : Controller
    {
        //
        // GET: /MembercartTypeChart/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult MembercartTypeChart()
        {
            List<SelectListItem> listLevel=new List<SelectListItem>();
            List<SelectListItem> listArea=new List<SelectListItem>();
            consumeSolutionManager CSManager=new consumeSolutionManager();
            AreasManager  AManager=new AreasManager();
            List<consumeSolution> CSList=CSManager.GetAll().ToList();
            List<Areas> Alist=AManager.GetAll().ToList();
            for( int i=0;i<CSList.Count();i++)
            {
                listLevel.Add(new SelectListItem{ Text=CSList[i].solutionName,Value=CSList[i].id.ToString()});
            }
            for(int i=0;i<Alist.Count();i++)
            {
                listArea.Add(new SelectListItem{ Text=Alist[i].AreaName,Value=Alist[i].id.ToString()});
            }
            ViewData["memberLevel"]=listLevel;
            ViewData["area"]=listArea;
            return View();
        }

        public ActionResult LoadPie()
        {
            int allNUm = 0;
            memberInfoManager MIManager = new memberInfoManager();
            consumeSolutionManager CSManager = new consumeSolutionManager();
            string sex = System.Web.HttpContext.Current.Request.QueryString["sex"];
            string memberLevel = System.Web.HttpContext.Current.Request.QueryString["memberLevel"];
            string area = System.Web.HttpContext.Current.Request.QueryString["area"]; 
           
            List<consumeSolution> CSList = CSManager.GetAll().ToList();
            List<string> name = new List<string>();
            List<string> bili = new List<string>();
            for (int i = 0; i < CSList.Count(); i++)
            {
                allNUm += MIManager.getNumByCardType(CSList[i].id);

            }
            if (!string.IsNullOrEmpty(sex) && !string.IsNullOrEmpty(memberLevel) && !string.IsNullOrEmpty(area))
            {
                int sexNum=Convert.ToInt32(sex);
                int memberLevelNum=Convert.ToInt32(memberLevel);
                int areaNum=Convert.ToInt32(area);
                IQueryable<memberInfo> memberList= MIManager.getByInput(sexNum, memberLevelNum, areaNum); 
                for (int i = 0; i < CSList.Count(); i++)
                {
                    int cardTypeId=CSList[i].id;
                   decimal num=decimal.Parse(memberList.Where(p => p.cardType ==cardTypeId ).Count().ToString());
                   if (Convert.ToInt32(num) != 0)
                   {
                       decimal xx = Convert.ToDecimal(num / allNUm);
                       string aa = (xx * decimal.Parse("100")).ToString();
                       decimal yy = 1 - xx;
                       string bb = (yy * decimal.Parse("100")).ToString();
                       bili.Add(aa);
                       name.Add(CSList[i].solutionName + "  比例：" + decimal.Parse(aa).ToString("F1") + "%");
                       bili.Add(bb);
                       name.Add("其他 "+ "  比例：" + decimal.Parse(bb).ToString("F1") + "%");
                   }
                }
            }
            else
            {
               
                for (int i = 0; i < CSList.Count(); i++)
                {

                    decimal num = decimal.Parse(MIManager.getNumByCardType(CSList[i].id).ToString());
                    if (Convert.ToInt32(num) != 0)
                    {
                        decimal xx = Convert.ToDecimal(num / allNUm);
                        string aa = (xx * decimal.Parse("100")).ToString();
                        bili.Add(aa);
                        name.Add(CSList[i].solutionName + "  比例：" + decimal.Parse(aa).ToString("F1") + "%");
                    }
                }
            }
           
            Chart chartPie = new Chart();
            chartPie.Height = 450;
            chartPie.Width = 850;
            chartPie.RenderType = System.Web.UI.DataVisualization.Charting.RenderType.ImageTag;
            chartPie.Palette = ChartColorPalette.BrightPastel;
            Title t = new Title("会员统计饼图", Docking.Top, new System.Drawing.Font("Trebuchet MS", 14, System.Drawing.FontStyle.Bold), System.Drawing.Color.FromArgb(26, 59, 105));
            chartPie.Titles.Add(t);
            chartPie.ChartAreas.Add("view");
            chartPie.Series.Add("Series1");
            chartPie.ChartAreas["view"].Area3DStyle.Enable3D =true;
            chartPie.ChartAreas[0].AxisX.Interval = 1;
            chartPie.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
            // CustomerChart.BackColor = System.Drawing.Color.Red;
            chartPie.BorderlineWidth = 2;
            chartPie.BorderColor = System.Drawing.Color.Black;
            chartPie.BorderlineDashStyle = ChartDashStyle.Solid;
            chartPie.BorderWidth = 1;
            chartPie.Series["Series1"].ChartType = SeriesChartType.Pie;
            chartPie.Series["Series1"]["PieLabelStyle"] = "Outside";
            chartPie.Series["Series1"]["PieLinelColor"] = "Red";
            chartPie.Series[0].Points.DataBindXY(name, bili);
            chartPie.DataBind();
            MemoryStream imageStream = new MemoryStream();
            chartPie.SaveImage(imageStream, ChartImageFormat.Png);
            imageStream.Position = 0;
            return new FileStreamResult(imageStream, "image/png");
        }
    }
}
